#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
数据集选择对话框
"""

from PyQt5.QtWidgets import (
    QDialog, QVBoxLayout, QHBoxLayout, QListWidget, QListWidgetItem, 
    QPushButton, QLabel, QSplitter, QWidget, QFormLayout, QAbstractItemView
)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QPixmap

class DatasetSelectDialog(QDialog):
    """数据集选择对话框"""
    
    def __init__(self, datasets, parent=None):
        """
        初始化对话框
        
        Args:
            datasets (list): 数据集列表
            parent (QWidget, optional): 父窗口
        """
        super().__init__(parent)
        
        # 保存数据集列表
        self.datasets = datasets
        self.selected_dataset = None
        
        # 设置对话框属性
        self.setWindowTitle("选择数据集")
        self.resize(600, 400)
        
        # 初始化UI
        self._setup_ui()
        
        # 如果有数据集，默认选择第一个
        if self.datasets and self.dataset_list.count() > 0:
            self.dataset_list.setCurrentRow(0)
    
    def _setup_ui(self):
        """设置界面"""
        # 主布局
        main_layout = QVBoxLayout(self)
        
        # 创建水平分割器
        splitter = QSplitter(Qt.Horizontal)
        
        # 左侧 - 数据集列表
        left_widget = QWidget()
        left_layout = QVBoxLayout(left_widget)
        
        # 标题
        left_layout.addWidget(QLabel("可用数据集:"))
        
        # 数据集列表
        self.dataset_list = QListWidget()
        self.dataset_list.setSelectionMode(QAbstractItemView.SingleSelection)
        self.dataset_list.currentItemChanged.connect(self._on_dataset_selected)
        
        # 填充数据集列表
        self._populate_dataset_list()
        
        left_layout.addWidget(self.dataset_list)
        
        # 右侧 - 数据集详情
        right_widget = QWidget()
        right_layout = QVBoxLayout(right_widget)
        
        # 标题
        right_layout.addWidget(QLabel("数据集详情:"))
        
        # 详情表单
        self.details_layout = QFormLayout()
        self.name_label = QLabel("-")
        self.algorithm_label = QLabel("-")
        self.created_label = QLabel("-")
        self.images_label = QLabel("-")
        self.labels_label = QLabel("-")
        
        self.details_layout.addRow("名称:", self.name_label)
        self.details_layout.addRow("算法类型:", self.algorithm_label)
        self.details_layout.addRow("创建时间:", self.created_label)
        self.details_layout.addRow("图像数量:", self.images_label)
        self.details_layout.addRow("标签数量:", self.labels_label)
        
        right_layout.addLayout(self.details_layout)
        right_layout.addStretch()
        
        # 添加部件到分割器
        splitter.addWidget(left_widget)
        splitter.addWidget(right_widget)
        
        # 设置分割比例
        splitter.setSizes([200, 400])
        
        main_layout.addWidget(splitter)
        
        # 按钮布局
        button_layout = QHBoxLayout()
        
        self.cancel_button = QPushButton("取消")
        self.cancel_button.clicked.connect(self.reject)
        
        self.open_button = QPushButton("打开")
        self.open_button.clicked.connect(self.accept)
        self.open_button.setEnabled(False)  # 默认禁用
        
        button_layout.addWidget(self.cancel_button)
        button_layout.addWidget(self.open_button)
        
        main_layout.addLayout(button_layout)
    
    def _populate_dataset_list(self):
        """填充数据集列表"""
        # 清空列表
        self.dataset_list.clear()
        
        # 添加数据集
        for dataset in self.datasets:
            item = QListWidgetItem(dataset.get("name", "未命名"))
            item.setData(Qt.UserRole, dataset)
            
            # 可以根据算法类型设置不同的图标
            algorithm = dataset.get("algorithm_type", "")
            if algorithm == "classification":
                # 设置图标（如果有）
                pass
            elif algorithm == "detection":
                # 设置图标（如果有）
                pass
            
            self.dataset_list.addItem(item)
    
    def _on_dataset_selected(self, current, previous):
        """数据集选择变更事件"""
        if current:
            # 获取选中的数据集
            dataset = current.data(Qt.UserRole)
            self.selected_dataset = dataset
            
            # 更新详情
            self.name_label.setText(dataset.get("name", "-"))
            self.algorithm_label.setText(dataset.get("algorithm_type", "-"))
            self.created_label.setText(dataset.get("created_at", "-"))
            self.images_label.setText(str(dataset.get("image_count", 0)))
            
            # 计算标签数量
            label_count = len(dataset.get("labels", []))
            self.labels_label.setText(str(label_count))
            
            # 启用打开按钮
            self.open_button.setEnabled(True)
        else:
            # 清空详情
            self.selected_dataset = None
            self.name_label.setText("-")
            self.algorithm_label.setText("-")
            self.created_label.setText("-")
            self.images_label.setText("-")
            self.labels_label.setText("-")
            
            # 禁用打开按钮
            self.open_button.setEnabled(False)
    
    def get_selected_dataset(self):
        """
        获取选中的数据集
        
        Returns:
            dict: 数据集信息，如果没有选择则返回None
        """
        return self.selected_dataset 